package com.wanmait.computershop.dao;

import com.wanmait.computershop.util.DBHelper;
import com.wanmait.computershop.vo.Product;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProductDAO {

    public static Product selectById(Integer pid)
    {
        QueryRunner queryRunner = new QueryRunner(DBHelper.getDruidDataSource());
        String sql = "select *,sell_point sellPoint,add_time addTime from product where id=?";
        try {
            return queryRunner.query(sql,new BeanHandler<>(Product.class),pid);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    public static List<Product> selectNew()
    {
        QueryRunner queryRunner = new QueryRunner(DBHelper.getDruidDataSource());
        String sql = "SELECT *,sell_point sellPoint,add_time addTime FROM product WHERE ENABLE =1 order BY add_time DESC LIMIT 0,4";
        try {
            return queryRunner.query(sql,new BeanListHandler<>(Product.class));
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    public static List<Product> selectHot()
    {
        QueryRunner queryRunner = new QueryRunner(DBHelper.getDruidDataSource());
        String sql = "SELECT *,sell_point sellPoint,add_time addTime FROM product WHERE ENABLE =1 order BY priority DESC LIMIT 0,4";
        try {
            return queryRunner.query(sql,new BeanListHandler<>(Product.class));
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;

    }


}
